/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Examples;

/**
 *
 * @author takeshi
 */

import java.util.*;
import org.apache.hadoop.io.*;
import homomorphism.list.*;
import utility.*;

public class MpsNew extends ThirdHomomorphismTheorem<LongWritable, LongPair> {

    // Computing mps and sum at a time.
    public LongPair fold(ArrayList<LongWritable> values) {
        long mps = 0;
        long sum = 0;
        for (LongWritable v : values) {
            sum += v.get();
            if (sum > mps) mps = sum;
        }

        return new LongPair(mps, sum);
    }

    // A right inverse of fold.
    public ArrayList<LongWritable> unfold(LongPair value) {
        long m = value.getFirst();
        long s = value.getSecond();

        ArrayList<LongWritable> rst = new ArrayList<LongWritable>();
        rst.add(new LongWritable(m));
        rst.add(new LongWritable(s-m));
        return rst;
    }
}
